package top.wardyan.wdev.dbdevassist.config;

import java.util.Properties;

import com.jfinal.kit.PropKit;

import top.wardyan.base.config.Datasource;
import top.wardyan.base.config.SystemConfig;
import top.wardyan.base.db.BaseSQLServerDatasource;
import top.wardyan.base.service.IGetIDService;
import top.wardyan.base.service.impl.GetIDServiceImpl;
import top.wardyan.wdev.dbdevassist.model.Objects;
import top.wardyan.wdev.dbdevassist.model.Proc;
import top.wardyan.wdev.dbdevassist.model.Table;
import top.wardyan.wdev.dbdevassist.model.Trigger;
import top.wardyan.wdev.dbdevassist.model.View;
import top.wardyan.wdev.dbdevassist.model.YTDic;
import top.wardyan.wdev.dbdevassist.model.YTDicItem;
import top.wardyan.wdev.dbdevassist.model.YTLog;
import top.wardyan.wdev.dbdevassist.model.YTName;

//import top.wardyan.wdev.dbassist.service.IMasterService;

import top.wardyan.wdev.dbdevassist.model.YTObject;
import top.wardyan.wdev.dbdevassist.model.YTObjectCol;
import top.wardyan.wdev.dbdevassist.model.YTObjectPara;
import top.wardyan.wdev.dbdevassist.model.YTType;
import top.wardyan.wdev.dbdevassist.model.YTUser;
import top.wardyan.wdev.dbdevassist.model._MappingKit;
import top.wardyan.wdev.dbdevassist.service.IDetailService;
import top.wardyan.wdev.dbdevassist.service.IMasterService;
import top.wardyan.wdev.dbdevassist.service.ISysService;
import top.wardyan.wdev.dbdevassist.service.IYTDicService;
import top.wardyan.wdev.dbdevassist.service.IYTLogService;
import top.wardyan.wdev.dbdevassist.service.IYTObjectColService;
import top.wardyan.wdev.dbdevassist.service.IYTObjectParaService;
import top.wardyan.wdev.dbdevassist.service.IYTObjectService;
import top.wardyan.wdev.dbdevassist.service.IYTTypeService;
import top.wardyan.wdev.dbdevassist.service.IYTUserService;
import top.wardyan.wdev.dbdevassist.service.impl.DetailServiceImpl;
import top.wardyan.wdev.dbdevassist.service.impl.MasterServiceImpl;
import top.wardyan.wdev.dbdevassist.service.impl.SysServiceImpl;
import top.wardyan.wdev.dbdevassist.service.impl.YTDicServiceImpl;
import top.wardyan.wdev.dbdevassist.service.impl.YTLogServiceImpl;
import top.wardyan.wdev.dbdevassist.service.impl.YTObjectColServiceImpl;
import top.wardyan.wdev.dbdevassist.service.impl.YTObjectParaServiceImpl;
import top.wardyan.wdev.dbdevassist.service.impl.YTObjectServiceImpl;
import top.wardyan.wdev.dbdevassist.service.impl.YTTypeServiceImpl;
import top.wardyan.wdev.dbdevassist.service.impl.YTUserServiceImpl;

public class DDA extends BaseSQLServerDatasource{
	
	// 配置文件
	public static final String APPCONFIGFILE = "conf/DBDevAssist/AppConfig.txt";
	public static final String SYSTEMCONFIGFILE = "conf/DBDevAssist/SystemConfig.txt";
	public static final String DATASOURCEFILE = "conf/DBDevAssist/datasource.conf";
	public static final String SQLTEMPLATE = "conf/DBDevAssist/sqltemplate.sql";
	
	// 
	public static final Properties PROP = PropKit.use(APPCONFIGFILE).getProperties();
	public static final SystemConfig SYSTEMCONFIG = SystemConfig.getInstance(SYSTEMCONFIGFILE);
	
	//
	public static final int SEARCHBOX = Integer.valueOf(PROP.getProperty("searchbox", "2"));
	
	// 数据源
	private static Datasource DATASOURCE;
	public static final String ADDRESS = getAddress();
	public static final Integer PORT = getPort();
	public static final String DBNAME = getDBName();
	public static final String USERNAME = getUsername();
	public static final String PASSWORD = getPassword();
	
	// service
	public IYTDicService dicService = new YTDicServiceImpl(YTDic.dao);
	public IDetailService<YTDicItem> dicItemService = new DetailServiceImpl<>(YTDicItem.dao);
	public IMasterService<YTName> nameService = new MasterServiceImpl<>(YTName.dao);
	
	public IGetIDService getIDService = GetIDServiceImpl.getInstance("dbo.yp_getid");
	
	
	public IYTObjectService objectService = new YTObjectServiceImpl(YTObject.dao);
	public IYTObjectColService objectColService = new YTObjectColServiceImpl(YTObjectCol.dao);
	public IYTObjectParaService objectParaService = new YTObjectParaServiceImpl(YTObjectPara.dao);
	
	public IYTLogService logService = new YTLogServiceImpl(YTLog.dao);
	//public IYPGetIDService getIDService = new YPGetIDServiceImpl();
	
	public IYTTypeService typeService = new YTTypeServiceImpl(YTType.dao);
	public IYTUserService userService = new YTUserServiceImpl(YTUser.dao);
	
	public ISysService<Proc> procService = new SysServiceImpl<>(Proc.dao);
	
	public ISysService<Table> tableService = new SysServiceImpl<>(Table.dao);
	public ISysService<View> viewService = new SysServiceImpl<>(View.dao);
	
	public ISysService<Objects> objectsService = new SysServiceImpl<>(Objects.dao);
	public ISysService<Trigger> triggerService = new SysServiceImpl<>(Trigger.dao);

	// 在线用户
	// 测试期间...
	public static YTUser user = null;
	
	// *** 
	
	private static Datasource getDatasource() {
		if(DATASOURCE == null) {
			DATASOURCE = Datasource.parse(DATASOURCEFILE);
		}
		// 开发时
		if(DATASOURCE == null) {
			DATASOURCE = new Datasource().setName("本地").setPwd("").setAddress("127.0.0.1").setPort(1433).setDbName("ERPDB").setUsername("sa").setPassword("Wardyan020598");
		}
		return DATASOURCE;
	}
	
	private static String getAddress() {
		if(PROP != null && PROP.getProperty("address") != null) {
			return PROP.getProperty("address");
		}else {
			return getDatasource().getAddress();
		}
	}
	private static Integer getPort() {
		if(PROP != null && PROP.getProperty("port") != null) {
			return Integer.valueOf(PROP.getProperty("port", "0"));
		}else {
			return getDatasource().getPort();
		}
	}
	private static String getDBName() {
		if(PROP != null && PROP.getProperty("dbName") != null) {
			return PROP.getProperty("dbName");
		}else {
			return getDatasource().getDbName();
		}
	}
	private static String getUsername() {
		if(PROP != null && PROP.getProperty("username") != null) {
			return PROP.getProperty("username");
		}else {
			return getDatasource().getUsername();
		}
	}
	private static String getPassword() {
		if(PROP != null && PROP.getProperty("password") != null) {
			return PROP.getProperty("password");
		}else {
			return getDatasource().getPassword();
		}
	}
	public static String getArpName() {
		if(PROP != null && PROP.getProperty("dbName") != null) {
			return PROP.getProperty("dbName");
		}else {
			return getDatasource().getDbName();
		}
	}
	private DDA() {
		//AppConfig.getInstance();
		super(ADDRESS, PORT, DBNAME, USERNAME, PASSWORD, DBNAME);
		//super("", 0, "", "", "", "");
		//this.getDp()
		this.getArp().addSqlTemplate(SQLTEMPLATE);
		if(PROP.get("devMode").equals("true"))
			user = this.userService.findByID(1);
		
	}
	
	
	
	
	
	
	
	
	
	
	
	private static DDA instance = null;
	public static DDA getInstance() {
		if(instance == null) {
			instance = new DDA();
		}
		return instance;
	}
	
	public static void main(String[] args) {
		//AppConfig c = AppConfig.getInstance("AppConfig.txt");
		//System.out.println(c.get("dbName"));
		//AppConfig.getInstance();
		System.out.println(ADDRESS);
		
	}

	@Override
	protected void mapping() {
		//new _MappingKit();
		new _MappingKit().mapping(this.getArp());
	}
	@Override
	protected String getPublicKey() {
		return SYSTEMCONFIG.PublicKey;
	}

}
